package com.dcy.znoa.controller.publiz;

import com.dcy.znoa.common.annotation.AuthenticationAPI;
import com.dcy.znoa.common.controller.BaseController;
import com.dcy.znoa.common.exception.BusinessException;
import com.dcy.znoa.common.util.pojo.JsonCommon;
import com.dcy.znoa.common.util.pojo.JsonForList;
import com.dcy.znoa.controller.publiz.model.duty.*;
import com.dcy.znoa.domain.publiz.Duty;
import com.dcy.znoa.service.publiz.DutyService;
import com.wordnik.swagger.annotations.ApiModel;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.lang.reflect.InvocationTargetException;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;
/**
 * @author pengyu
 */
//职位 controller
@Api(value="职位1",description="职位相关接口")
@ApiModel(value="职位2",description="职位相关")
@Controller
@RequestMapping("duty")
public class DutyController extends BaseController {
    @Autowired
    DutyService dutyService;
    //service的接口最好不要返回你的jsonComm,如果这样sercice的方法就不好重用
    @AuthenticationAPI
    @ResponseBody
    @RequestMapping("/selectList")
    @ApiOperation(value="查询职位列表",httpMethod="GET",notes="get JsonForList by SelectDutyModel",response=Duty.class,tags="查询职位1111")
    public JsonForList selectList(@ModelAttribute @Valid SelectDutyModel model) throws BusinessException, InvocationTargetException, IllegalAccessException {
        return dutyService.selectList(model);
    }
//    @Validated @ModelAttribute @ApiParam @Valid

    @AuthenticationAPI
    @ResponseBody
    @RequestMapping("/insert")
    @ApiOperation(value="新增职位",httpMethod="POST",notes="POST JsonForList by AddDutyModel",response=Duty.class)
    public JsonCommon insert(@Valid AddDutyModel model,BindingResult bindingResult) throws BusinessException, InvocationTargetException, IllegalAccessException {
        if (bindingResult.hasErrors()){
            return  new JsonCommon("404",bindingResult.getFieldError().getDefaultMessage());
        }
        model.setCreateOperId(getCurrentUser().getOperId());
        Duty cp = new Duty();
        BeanUtils.copyProperties(cp, model);
        return dutyService.insert(cp);
    }

    @AuthenticationAPI
    @ResponseBody
    @RequestMapping("/updateById")
    public JsonCommon updateById(@Valid UpdateDutyModel model,BindingResult bindingResult) throws BusinessException, InvocationTargetException, IllegalAccessException {
        if (bindingResult.hasErrors()){
            return  new JsonCommon("404",bindingResult.getFieldError().getDefaultMessage());
        }
        model.setUpdateOperId(getCurrentUser().getOperId());
        Duty cp = new Duty();
        BeanUtils.copyProperties(cp, model);
        return dutyService.updateById(cp);
    }

    @AuthenticationAPI
    @ResponseBody
    @RequestMapping("/logicDeleteById")
    public JsonCommon logicDeleteById(@Valid LogicDelDutyModel model,BindingResult bindingResult) throws BusinessException, InvocationTargetException, IllegalAccessException {
        if (bindingResult.hasErrors()){
            return  new JsonCommon("404",bindingResult.getFieldError().getDefaultMessage());
        }
        model.setUpdateOperId(getCurrentUser().getOperId());
        Duty cp = new Duty();
        BeanUtils.copyProperties(cp, model);
        return dutyService.updateById(cp);
    }

    @AuthenticationAPI
    @ResponseBody
    @RequestMapping("/logicUnDelById")
    public JsonCommon logicUnDelById(@Valid LogicUnDelDutyModel model, BindingResult bindingResult) throws BusinessException, InvocationTargetException, IllegalAccessException {
        if (bindingResult.hasErrors()){
            return  new JsonCommon("404",bindingResult.getFieldError().getDefaultMessage());
        }
        model.setUpdateOperId(getCurrentUser().getOperId());
        Duty cp = new Duty();
        BeanUtils.copyProperties(cp, model);
        return dutyService.updateById(cp);
    }
    @AuthenticationAPI
    @ResponseBody
    @RequestMapping("/deleteById")
    public JsonCommon deleteById(@Valid LogicDelDutyModel model,BindingResult bindingResult) throws BusinessException, InvocationTargetException, IllegalAccessException {
        if (bindingResult.hasErrors()){
            return  new JsonCommon("404",bindingResult.getFieldError().getDefaultMessage());
        }
        model.setUpdateOperId(getCurrentUser().getOperId());
        Duty cp = new Duty();
        BeanUtils.copyProperties(cp, model);
        return dutyService.deleteById(cp);
    }
}
